package xupt.se.ttms.view.sellticket;




import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.w3c.dom.css.ElementCSSInlineStyle;

import xupt.se.ttms.model.Movie;
import xupt.se.ttms.model.Play;
import xupt.se.ttms.model.Seat;
import xupt.se.ttms.model.Studio;
import xupt.se.ttms.model.Ticket;
import xupt.se.ttms.service.PlaySrv;
import xupt.se.ttms.service.SeatSrv;
import xupt.se.ttms.service.StudioSrv;
import xupt.se.ttms.service.TicketSrv;
import xupt.se.ttms.view.customer.Customer_MainUITmpl;

import java.awt.Color;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TicketOkUI{
	public static JLabel a2;
	public static JLabel b2;
	public static JLabel c2;
	public static JLabel d2;
	public static JLabel e2;
	public static JLabel f2;
	public static JLabel g2;
	public static JLabel h2;
	public static JFrame frame;
	public static Play play=null;
	public static int studio_id=0;
	public static TicketSrv ticketSrv=null;
	public static Ticket ticket=null;
	public  TicketOkUI(Movie movie,int[][] st,String name,String time,int k,int p) throws Exception
	{
		
		play=init(movie, name, time);
		frame = new JFrame();
		frame.setSize(850,500);
		frame.setTitle("购票界面");
		frame.setBounds(0, 0, 850, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		ImageIcon icon=new ImageIcon("img/2.png");
		JLabel x = new JLabel(icon);
		icon.setImage(icon.getImage().getScaledInstance(850,70,Image.SCALE_DEFAULT));
		JPanel a=new JPanel();
		a.setBackground(Color.decode("#FFe4c4"));
		a.setLayout(null);
		x.setBounds(0,0,850,70);
		a.add(x);
		frame.add(a);
		a2=new JLabel();
		a2.setBounds(375,100,200,25);
		a.add(a2);
		JLabel b1=new JLabel("电影名称 :");
		b1.setBounds(300,130,75,25);
		a.add(b1);
		b2=new JLabel(movie.getName());
		b2.setBounds(375,130,200,25);
		a.add(b2);
		JLabel c1=new JLabel("演出时长:");
		c1.setBounds(300,160,75,25);
		a.add(c1);
		c2=new JLabel(movie.getTime());
		c2.setBounds(375,160,200,25);
		a.add(c2);
		String str = "";
		for(int i=0;i<k;i++)
		{
			str = str+"  "+st[i][0];
		}
		JLabel d1=new JLabel("座位行号:");
		d1.setBounds(300,190,75,25);
		a.add(d1);
		d2=new JLabel(str);
		d2.setBounds(375,190,200,25);
		a.add(d2);
		String strs = "";
		for(int i=0;i<k;i++)
		{
			strs = strs+"  "+st[i][1];
		}
		JLabel e1=new JLabel("座位列号:");
		e1.setBounds(300,220,75,25);
		a.add(e1);
		e2=new JLabel(strs);
		e2.setBounds(375,220,200,25);
		a.add(e2);
		JLabel f1=new JLabel("演出时间:");
		f1.setBounds(300,250,75,25);
		a.add(f1);
		f2=new JLabel(time);
		f2.setBounds(375,250,200,25);
		a.add(f2);
		JLabel g1=new JLabel("演出厅名称:");
		g1.setBounds(300,280,100,25);
		a.add(g1);	
		g2=new JLabel(name);
		g2.setBounds(375,280,200,25);
		a.add(g2);
		String qws =""+ 58 * st.length;
		JLabel h1=new JLabel("票价:");
		h1.setBounds(300,310,100,25);
		a.add(h1);	
		h2=new JLabel(qws);
		h2.setBounds(375,310,200,25);
		a.add(h2);
		JButton enter=new JButton("确认");
		enter.setBounds(375,360,90,30);
		enter.setFocusPainted(false);
		enter.setMargin(new Insets(0,0,0,0));
		enter.setContentAreaFilled(false);
		enter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				
				SeatSrv seatSrv=null;
				Seat seat=null;
				List<Seat> seats;
				for(int i=0;i<k;i++)
				{
					
					int rows=0;
					int cols=0;
					for(int j=0;j<p;j++)
					{
						if(j==0)
						{
							rows=st[i][j];
						}
						else if(j==1)
						{
							cols=st[i][j];
						}
					}
					ticketSrv=new TicketSrv();
					ticket=new Ticket();
					
					ticket.setMv_name(movie.getName());
					ticket.setMv_time(movie.getTime());
					ticket.setPlay_time(play.getPlay_time());
					ticket.setStudio_name(name);
					ticket.setPrice(play.getPlay_price());
					ticket.setSeat_row(rows);
					ticket.setSeat_col(cols);
					
				
					try {
						TicketSource source=new TicketSource();
						new Thread(source).start();
						//ticketSrv.add(ticket);
						
						
					} catch (Exception e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
					seatSrv=new SeatSrv();
					seat=new Seat();
					seats=new ArrayList<>();
					try {
						seats=seatSrv.FetchSeat(studio_id,rows, cols);
						if(seats.size()<1)
							return;
						int seat_id=seats.get(0).getSeat_id();
						seat.setSeat_id(seat_id);
						seat.setStudio_id(studio_id);
						seat.setRow(rows);
						seat.setCol(cols);
						seat.setState("已售");
						seatSrv.modify(seat);
					} catch (Exception e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}
				JOptionPane.showConfirmDialog(null, "购票成功");
				new Customer_MainUITmpl("");
			}
		});
		a.add(enter);
		JButton enter1=new JButton("返回");
		enter1.setBounds(485,360,90,30);
		enter1.setFocusPainted(false);
		enter1.setMargin(new Insets(0,0,0,0));
		enter1.setContentAreaFilled(false);
		enter1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
			}
		});
		
		a.add(enter1);
		frame.setVisible(true);
		frame.setLayout(null);
		
		
	}
	
	public Play init(Movie movie,String name, String time) throws Exception
	{
		StudioSrv studioSrv=new StudioSrv();
		List<Studio> studios=new ArrayList<>();
		PlaySrv playSrv=new PlaySrv();
		List<Play> plays=new ArrayList<>();
		studios=studioSrv.Fetch(name);
		if(studios.size()<1)
			return null;
		studio_id=studios.get(0).getID();
		plays=playSrv.FetchPlay(movie.getMv_id(), studios.get(0).getID(), time);
		if(plays.size()<1)
			return  null;
		return plays.get(0);
	}

	class TicketSource implements Runnable {
		public void run() {
			try {
				this.sale();
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

		private synchronized void sale() throws Exception {
			// TODO 自动生成的方法存根
			
				if(play.getTicket_count()>0)
				{
					ticketSrv.add(ticket);
				}
			}
		
	}
}
